package main

import (
	"log"
	"os"
	"time"

	"github.com/urfave/cli"
)

// Memo represents a memo item
type Memo struct {
	ID         int
	Text       string
	CreatedAt  time.Time
	ModifiedAt time.Time
}

var memos []Memo
var lastID int

func main() {
	loadMemos() // 加载备忘录数据

	app := cli.NewApp() //初始化app类型数据
	app.Name = "Memo"
	app.Usage = "A simple command-line memo application"
	app.Version = "1.0.0"

	app.Commands = []cli.Command{
		{
			Name:    "add",
			Aliases: []string{"a"},
			Usage:   "Add a new memo",
			Action:  addMemo,
		},
		{
			Name:    "list",
			Aliases: []string{"l"},
			Usage:   "List all memos",
			Action:  listMemos,
		},
		{
			Name:    "delete",
			Aliases: []string{"d"},
			Usage:   "Delete a memo by ID",
			Action:  deleteMemo,
			Flags: []cli.Flag{
				cli.IntFlag{
					Name:  "id",
					Usage: "ID of the memo to delete",
				},
			},
		},
		{
			Name:    "search",
			Aliases: []string{"s"},
			Usage:   "Search memos by keyword",
			Action:  searchMemos,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "keyword, k",
					Usage: "Keyword to search for in memos",
				},
			},
		},
		{
			Name:    "update",
			Aliases: []string{"u"},
			Usage:   "Update a memo by ID",
			Action:  updateMemo,
			Flags: []cli.Flag{
				cli.IntFlag{
					Name:  "id",
					Usage: "ID of the memo to update",
				},
				cli.StringFlag{
					Name:  "text, t",
					Usage: "New text for the memo",
				},
			},
		},
	}

	err := app.Run(os.Args) //没有启动起来就直接报错；
	if err != nil {
		log.Fatal(err)
	}
}
